<?php
namespace app\shop\controller;
use app\common\controller\Common;
use app\shop\model\ActivityModel;
use app\common\model\Activity as ActivityDb;
use app\shop\model\AddOrderModel;
use app\common\model\Goods;
use app\common\model\Shop;
use think\Exception;
use think\Request;

/**
* 活动管理
*/
class Activity extends Common
{
	/******************满减活动*********************/
    /**
    * 新增满减活动
    */
    public function mjaddactiv()
    {
        $info = Request::instance()->param();
        if(request()->isPost())
		{
			try{
				return json(ActivityModel::setActivity($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
        $this->assign('ty',$info['ty']);
        return $this->fetch();
    }
    /**
    * 满减活动列表
    */
    public function mjactivlist()
    {
        $info = Request::instance()->param();
        if(request()->isPost())
		{
			return ActivityModel::activityList($info);
		}
        $this->assign('ty',$info['ty']);
        return $this->fetch();
    }
    /**
    * 编辑活动
    */
    public function editactivity()
    {
        $info = Request::instance()->param();
        if(request()->isPost())
		{
			try{
				return json(ActivityModel::editActivity($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
        
        $this->assign('ty',$info['ty']);
        $data = (new ActivityDb())->get($info['id']);
        $data['gname'] = [];
        if($info['ty'] == 1 || $info['ty'] == 2)
        {
            $data['gname'] = (new Goods())->fieldGoods(['goods_id'=>$data['zs_goods']],'goods_name');
        }
        if($info['ty'] == 3)
        {
            $goods = ActivityModel::stockGoodsList($data['zs_goods']);
            $goodsname = [];
            foreach($goods as $vo)
            {
                $goodsname[] = $vo['goods_name'];
            }
            $data['goodsname'] = $goodsname;
            $data['gname'] = json_encode($goods,JSON_UNESCAPED_UNICODE);
        }
        $data['start'] = date('Y-m-d',$data['start']);
        $data['end']   = date('Y-m-d',$data['end']);
        $this->assign('info',$data);
        return $this->fetch();
    }
    /**
    * 删除活动信息
    */
    public function delactivity()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(ActivityModel::delActivity($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
    /**
    * 活动商品
    */
    public function activitygoods()
    {
        if(request()->isPost())
		{
            $info = Request::instance()->param();
			return ActivityModel::stockGoods($info);
		}
        return $this->fetch(); 
    }
    /**
    * 活动商品
    */
    public function activitygoods3()
    {
        if(request()->isPost())
		{
            $info = Request::instance()->param();
			return ActivityModel::stockGoods($info);
		}
        return $this->fetch(); 
    }
    /**
    * 总部活动
    */
    public function headactivity()
    {
        if(request()->isPost())
		{
			$info = Request::instance()->param();
			return ActivityModel::headactivity($info);
		}
       $this->assign('shop',(new Shop())->shopList(['is_show'=>0],'shop_id,shop_name'));
       return $this->fetch(); 
    }
    /**
    * 活动详情
    */
    public function headainfo()
    {
        $info = Request::instance()->param();
        
        list($info,$keliu,$onum,$tota,$ctota,$xtota) = ActivityModel::headaInfo($info);
        $this->assign('info',$info);
        $this->assign('keliu',$keliu);
        $this->assign('onum',$onum);
        $this->assign('tota',$tota);
        $this->assign('ctota',$ctota);
        $this->assign('xtota',$xtota);
        return $this->fetch(); 
    }
}
